package membertask

import (
	"context"
	// "fmt"
	"strings"

	"mall/common/modelx"
	"mall/common/resultx"
	"mall/service/backend/common/utils/pointy"
	"mall/service/setting/model"
	"mall/service/setting/rpc/internal/svc"
	"mall/service/setting/rpc/types/setting"

	// "github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetMemberTaskListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetMemberTaskListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMemberTaskListLogic {
	return &GetMemberTaskListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetMemberTaskListLogic) GetMemberTaskList(in *setting.BaseListReq) (*setting.MemberTaskListResp, error) {
	if in.Filter == nil || len(*in.Filter) == 0 {
		return nil, resultx.StatusError(resultx.REUQEST_PARAM_ERROR, "")
	}

	preloadMap, _ := modelx.ParsePreloadAndEagerLoad(in.Preload, in.EagerLoad)
	// logrus.Info(fmt.Sprintf("GetMemberTaskList preloadMap: %+v", preloadMap))
	// map[memberTask:[Id Name Slug Desc PicId PicUrl Gems]]
	// logrus.Info(fmt.Sprintf("GetMemberTaskList eagerLoadMap: %+v", eagerLoadMap))
	// map[]

	resp := &setting.MemberTaskListResp{}

	memberTaskEntityName := model.RpcEntityNames.MemberTask
	_, hasSettingMap := model.RpcEntityPreloadMap[memberTaskEntityName]
	if !hasSettingMap {
		return resp, nil
	}

	memberTaskRepositoryName := model.RepositoryNames.MemberTask
	columnMap, hasColumnMap := model.RepositoryPreloadMap[memberTaskRepositoryName]
	if !hasColumnMap {
		return resp, nil
	}

	var selectCols []string
	if preloadFields, hasPreloadFields := preloadMap[memberTaskEntityName]; hasPreloadFields {
		selectCols = modelx.ParseSelectColumns(preloadFields, columnMap)
	}

	filterMap := modelx.ParseFilter(in.Filter, columnMap)
	// logrus.Info(fmt.Sprintf("GetMemberTaskList filterMap: %+v", filterMap))
	// map[status:map[equalTo:[1]]] 
	if filterMap == nil {
		return nil, resultx.StatusError(resultx.REUQEST_PARAM_ERROR, "")
	}

	orderBy := modelx.ParseSorter(in.Sorter, columnMap)
	// logrus.Info(fmt.Sprintf("GetMemberTaskList orderBy: %s", orderBy))
	// sort ASC

	if len(orderBy) == 0 {
		orderBy = model.SysMemberTaskColumns.Sort
	}

	res, count, err := l.svcCtx.MemberTask.GetList(l.ctx, model.M{
		"offset":  in.Offset,
		"limit":   in.Limit,
		"orderBy": orderBy,
		"select":  strings.Join(selectCols, ","),
	}, 2, filterMap)
	if err != nil {
		return nil, err
	}

	hasNextPage, hasPreviousPage := modelx.ParseCount(count)
	resp.HasNextPage = &hasNextPage
	resp.HasPreviousPage = &hasPreviousPage

	if len(res) > 0 {
		for _, v := range res {
			resp.List = append(resp.List, &setting.MemberTaskInfo{
				Id:          &v.ID,
				Name:        &v.Name,
				Slug:        &v.Slug,
				Description: &v.Description,
				Remark:      &v.Remark,
				PicId:       &v.PicID,
				PicUrl:      &v.PicURL,
				Gems:        pointy.GetPointer(uint32(v.Gems)),
				Growth:      pointy.GetPointer(uint32(v.Growth)),
				Integrals:   pointy.GetPointer(uint32(v.Integrals)),
				Sort:        pointy.GetPointer(uint32(v.Sort)),
				Status:      pointy.GetPointer(uint32(v.Status)),
				CreatedAt:   pointy.GetPointer(modelx.FormatTime(v.CreatedAt, "")),
				UpdatedAt:   pointy.GetPointer(modelx.FormatTime(v.UpdatedAt, "")),
				DeletedAt:   pointy.GetPointer(modelx.FormatNullDotTime(v.DeletedAt, "")),
			})
		}
	}

	return resp, nil
}
